package com.atguigu.gulimall.controller;

import com.atguigu.gulimall.common.utils.R;
import com.atguigu.gulimall.service.SeckillService;
import com.atguigu.gulimall.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class KillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀商品信息
     *
     * @return
     */
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> redisToList = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data", redisToList);
    }

    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSecKillInfo(skuId);

        return R.ok().put("data", to);
    }

    /**
     *
     * @param killId  sessionId_skuId
     * @param key     code
     * @param num     count
     * @return
     */
    @GetMapping("/kill")
    public R kill(@RequestParam("killId") String killId,
                  @RequestParam("key")String key,
                  @RequestParam("num")Integer num) {
        seckillService.kill(killId,key,num);
        return R.ok();
    }
}
